package com.mswyt.utils.config.scan;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.mswyt.utils.base.MsAssertUtils;
import com.mswyt.utils.config.cache.MsCache;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化时候，对使用工具类的一些注解进行校验
 *
 * @ClassName: MsSpringListener
 * @Description: 监听spring初始化结束
 */
@Component
public class MsSpringListener implements ApplicationListener<ContextRefreshedEvent> {
    private static Boolean isCheckCrud = false;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!isCheckCrud) {
            List<Class<?>> classList = MsCache.crudClassList;
            for (Class<?> aClass : classList) {
                TableInfo tableInfo = TableInfoHelper.getTableInfo(aClass);
                if (tableInfo == null) {
                    MsAssertUtils.throwException("实体类：" + aClass.getName() + "使用了@MsCrud注解，该实体类必须有对应的mapper层接口实现，例如：inter MapperXXXX extends BaseMapper<实体类> ");
                }
            }
            isCheckCrud = true;
        }
    }
}
